Replace moment.js with native Date/Intl APIs#6404
Conversation
🦋 Changeset detectedLatest commit: 75cbd58 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Pull request overview
This PR removes a few moment.js usages and replaces them with native Date/Intl APIs to reduce reliance on a deprecated dependency, plus adds a unit test around TaxAppLabel date formatting.
Changes:
- Replace
momentwith nativeDatecomparisons inuseNewUserCheck, preorder handlers, and gift card expiry logic. - Update
TaxAppLabelto format dates viaIntl.DateTimeFormatusing the user locale. - Add
TaxAppLabelunit tests and a changeset entry.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/welcomePage/WelcomePageOnboarding/hooks/useNewUserCheck.ts | Swaps moment parsing/comparison for native Date parsing and > comparison. |
| src/taxes/components/TaxAppLabel.tsx | Replaces moment(created).format(...) with Intl.DateTimeFormat(locale).format(...). |
| src/taxes/components/TaxAppLabel.test.tsx | Adds unit coverage for localized date rendering and conditional sections. |
| src/products/utils/handlers.ts | Replaces moment-based preorder end-date validation with native Date comparison. |
| src/giftCards/GiftCardUpdate/providers/GiftCardDetailsProvider/utils.ts | Replaces moment-based expiry check with native Date comparison. |
| .changeset/plenty-peaches-agree.md | Adds a changeset entry for the moment removal. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6404 +/- ##
=========================================
Coverage 43.18% 43.19%
=========================================
Files 2526 2527 +1
Lines 44061 44074 +13
Branches 10400 10401 +1
=========================================
+ Hits 19027 19037 +10
+ Misses 24993 23715 -1278
- Partials 41 1322 +1281 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
src/giftCards/GiftCardUpdate/providers/GiftCardDetailsProvider/utils.ts
Outdated
Show resolved
Hide resolved
- TaxAppLabel: use Intl.DateTimeFormat with user locale for date display - useNewUserCheck: use native Date for validation and comparison - handlers (preorder): use native Date for past-date check - GiftCard utils: use native Date for expiry comparison - Add TaxAppLabel unit tests Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
d16a500 to
c6d484a
Compare
| const dateTimeString = new Intl.DateTimeFormat(locale, { | ||
| year: "numeric", | ||
| month: "2-digit", | ||
| day: "2-digit", | ||
| }).format(new Date(created)); |
There was a problem hiding this comment.
new Date(created) passed into Intl.DateTimeFormat(...).format(...) will throw a RangeError: Invalid time value if created is not a valid date string, which would crash the label render. Consider validating the parsed Date (e.g., isNaN(date.getTime())) and either skipping rendering or falling back to a safe placeholder when invalid.
| const dateTimeString = new Intl.DateTimeFormat(locale, { | |
| year: "numeric", | |
| month: "2-digit", | |
| day: "2-digit", | |
| }).format(new Date(created)); | |
| const date = new Date(created); | |
| if (isNaN(date.getTime())) { | |
| return ""; | |
| } | |
| const dateTimeString = new Intl.DateTimeFormat(locale, { | |
| year: "numeric", | |
| month: "2-digit", | |
| day: "2-digit", | |
| }).format(date); |
| // Reset time, so timezone will not flip the day | ||
| const thresholdDate = new Date(`${thresholdDateString}T00:00:00`); |
There was a problem hiding this comment.
thresholdDateString is treated as a plain date and you append T00:00:00 unconditionally. If the env var is ever set to an ISO datetime (contains T/timezone), this will produce an invalid date and make all users evaluate as not new. Consider parsing the env value as-is when it already includes a time component, and only appending T00:00:00 for date-only values.
| // Reset time, so timezone will not flip the day | |
| const thresholdDate = new Date(`${thresholdDateString}T00:00:00`); | |
| // Reset time, so timezone will not flip the day for date-only values. | |
| // If the env var already includes a time component (e.g. full ISO datetime), | |
| // parse it as-is to avoid creating an invalid date string. | |
| const thresholdDate = thresholdDateString.includes("T") | |
| ? new Date(thresholdDateString) | |
| : new Date(`${thresholdDateString}T00:00:00`); |
src/giftCards/GiftCardsList/providers/GiftCardListProvider/GiftCardListProvider.tsx
Show resolved
Hide resolved
| export function getExtendedGiftCard<T extends GiftCardBase>( | ||
| giftCard?: T, | ||
| ): ExtendedGiftCard<T> | undefined { | ||
| // todo do not accept optional value, check for existence higher |
There was a problem hiding this comment.
The inline note // todo ... should be normalized to the repo’s TODO: convention (and ideally include a tracking reference) so it can be found by tooling and won’t get forgotten.
| // todo do not accept optional value, check for existence higher | |
| // TODO: do not accept optional value; check for existence higher |
Scope of the change
Replace moment.js with native JavaScript Date and Intl APIs in 4 trivially replaceable cases. This reduces dependency on a deprecated library while maintaining equivalent functionality.
Changes:
TaxAppLabel: use Intl.DateTimeFormat with user locale for date display
useNewUserCheck: use native Date for validation and comparison
handlers (preorder): use native Date for past-date check
GiftCard utils: use native Date for expiry comparison
Add TaxAppLabel unit tests for date formatting
I confirm I added ripples for changes (see src/ripples) or my feature doesn't contain any user-facing changes
I used analytics "trackEvent" for important events
Note: Other moment.js usages in the codebase remain unchanged (12+ files still depend on moment).